package com.tbs.parkingsaleapp.UI;

import abstergo.tbs.betterasyc.Handler.HandlerMananger;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Message;
import android.view.TextureView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.tbs.parkingsaleapp.SystemManager.Handler.HandlerConst;
import com.tbs.parkingsaleapp.UI.Listenners.FaceDetectionListenner;
import com.tbs.parkingsaleapp.R;

public class FaceTakeActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
    public static  int SUCCESS=1001;
    public static  String ResultName="face";
    public static boolean safeToTakePicture = false;
    private static final int DelayTime = 3000;

    Camera camera;
    TextureView ca;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_face_take);
        ca=findViewById(R.id.cam);
        ca.setSurfaceTextureListener(this);
        ca.setClipToOutline(true);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int i2, int i1) {
        try {
            int range= Camera.getNumberOfCameras();
            Camera.CameraInfo info=new Camera.CameraInfo();
            if(range>1)
            {
                for(int i=0;i<range;i++)
                {
                    Camera.getCameraInfo(i,info);
                    if(info.facing==Camera.CameraInfo.CAMERA_FACING_FRONT)
                    {
                        camera=Camera.open(i);
                        break;
                    }
                }
            }
            else if(range==1)
                camera=Camera.open();
            Camera.Parameters params = camera.getParameters();
            if(params.getMaxNumDetectedFaces()>0)
            {
                camera.setFaceDetectionListener(new FaceDetectionListenner(this));
                camera.startFaceDetection();
            }
            camera.setPreviewTexture(ca.getSurfaceTexture());
            camera.setDisplayOrientation(90);
            camera.startPreview();
            Message msg=new Message();
            msg.what=HandlerConst.SAFETOTAKE;
            HandlerMananger.Instance().SendMsg(msg,DelayTime);
            Toast.makeText(this,"将于3秒后开始拍照",Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
        camera.stopFaceDetection();
        camera.stopPreview();
        safeToTakePicture=false;
        camera.release();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {

    }
}